
var useLocal = true;
var urlRoot = "http://47.104.102.188:7979";

$(function(){
    var config = getConfig();
    useLocal = config["proj"]["local"];
    urlRoot = useLocal ? config["proj"]["local_root"] : config["proj"]["url_root"];
    console.log("proj-url: " + urlRoot);

    getProjects();
});

document.oncontextmenu = function(){
    return false;
}

function getProjects() {
    if(useLocal)
    {
        var url = urlRoot + "/bio-analysis-web/datas/my_projects.json";
        console.log("project url: " + url);
        $.get(url, function(data) {
            //var projData = JSON.parse(data);
            //console.log(projData);
        });

        var projJson = {
            "datas": [
                {
                    "proj_name": "01",
                    "app_name": "生物多样性分析",
                    "proj_time": "3333",
                    "complete": true
                },
                {
                    "proj_name": "02",
                    "app_name": "生物多样性分析",
                    "proj_time": "2222",
                    "complete": false
                },
                {
                    "proj_name": "03",
                    "app_name": "生物多样性分析",
                    "proj_time": "3333",
                    "complete": true
                }
            ]
        }

        fillTable(projJson.datas);
    }
    else
    {
        var url = urlRoot + "/cnafInterfaceService/thing/workUnitInfoAction.do";
        var param = {"unitCode":unitCode,"condition":3};
        param = JSON.stringify(param);
        //console.log("camlist url: " + url);
        $.post(url, param, function(data){
                $("#text").html(data);
                //console.log(data);
                newCamJson = JSON.parse(data);
                renderLi(newCamJson);
                //fillCamList(JSON.parse(data));
            }
        );
    }
}

var projItemList = [];

function fillTable(projData) {
    console.log(projData);
    for(var i=0; i<projData.length; ++i)
    {
        var it = projData[i];
        /*
        console.log(it.proj_name);
        console.log(it.app_name);
        console.log(it.proj_time);
        console.log(it.complete);
        */
        var item = document.createElement('tr');
        item.setAttribute("id", it.proj_name);
        item.id = it.proj_name;
        item.innerHTML = `<td>${it.proj_name}</td>
            <td>${it.app_name}</td>
            <td>${it.proj_time}</td>
            <td>${it.complete}</td>
            <td><input type="button" class="download_btn" value="下载"/></td>
            <td><input type="button" class="del_btn" value="X" onclick="delItem(this)"/></td>`;
        $('#proj_table').append(item);


        projItemList.push(item);


        /*
        var btn = document.createElement("input");
        btn.setAttribute("type", "button");
        btn.setAttribute("value", "放置");
        btn.setAttribute("id", id);
        btn.setAttribute("class", "btn");
        btn.style.margin = "1px 1px 1px 15px";
        btn.setAttribute("onmousedown", "sendCreateMesssage(id)");
        //console.log(id);
        node.target.appendChild(btn);
        */

    }

    console.log(projItemList);
}

function searchProject()
{
    var text = $("#searchInput").val().trim();
    if(text == "") {
        for(var i=0; i<projItemList.length; ++i) {
            projItemList[i].hidden = false;
           // projItemList[i].css("display","block");
        }
    } else {
        for(var i=0; i<projItemList.length; ++i)
        {
            var item = projItemList[i];
            if(item.id.indexOf(text) != -1)
            {
                item.hidden = false;
                //item.css("display","block");
            } else {
                item.hidden = true;
                //item.css("display","none");
            }
        }
    }


}

function searchProjectChange()
{
    var text = $("#searchInput").val().trim();
    if(text == "") {
        for(var i=0; i<projItemList.length; ++i) {
            projItemList[i].hidden = false;
            //projItemList[i].css("display","block");
        }
    }
}

// 删除对话框 -----------------
var delProjName;
function delItem(btn) {
    delProjName = btn.parentNode.parentNode.id;
    var message = "你确认删除项目："+ delProjName + " 吗？";

    $('.dialog-message').html(message);
    // 显示遮罩和对话框
    $('.wrap-dialog').removeClass("hide");
}

function delOk()
{
    $('.wrap-dialog').addClass("hide");
    if(delProjName)
    {
        console.log("del  " + delProjName);
    }

    delProjName = null;
}

function delCancel() {
    $('.wrap-dialog').addClass("hide");
    if(delProjName)
    {
        console.log("cancel  " + delProjName);
    }
    delProjName = null;
}
